package com.coollang.smartbra.presenter;

import com.coollang.smartbra.activity.Welcome2Activity;
import com.coollang.smartbra.base.MyApplication;
import com.coollang.smartbra.model.ChangeUserInfoModel;
import com.coollang.smartbra.model.LogInDataBean;
import com.coollang.smartbra.utils.TimeUtils;

import java.util.HashMap;
import java.util.Map;

/**
 * @Auther: Timmy
 * @Data: 2017/7/20
 * @Fuction:
 */

public class Welcome2ActivityPresenter extends BasePresenter<ChangeUserInfoModel> {

    private Welcome2Activity mActivity;
    private Map<String, String> mMap = new HashMap<>();
    private int requestMode;
    private int mBelongType;


    public Welcome2ActivityPresenter(Welcome2Activity activity) {
        super(ChangeUserInfoModel.class);
        mActivity = activity;
    }

    @Override
    public void setRequestInfo() {
    }

    @Override
    protected Map<String, String> getData() {
        return mMap;
    }

    @Override
    protected void parsrData(ChangeUserInfoModel model) {
        String code = model.getCode();
        if ("200".equals(code)) {
            switch (requestMode) {
                case 1:
                    // 设置难度
                    break;
                case 2:
                    // 设置强度
                    break;
                case 3:
                    // 设置用户信息
                    saveUserData(model.getData());
                    break;
                case 4:
                    mActivity.setSucceed();
                    break;
            }

        }
    }

    // 请求修改难度
    public void requestGoal(int goal) {
        requestMode = 1;
        mBelongType = goal;
        mMap.clear();
        mMap.put("username", MyApplication.mLogInDataBean.getUsername());
        mMap.put("goal", goal + "");
        setBaseOkgo("user/set-goal");
    }

    // 请求修改强度
    public void requestStrength(int strength) {
        requestMode = 2;
        mMap.clear();
        mMap.put("username", MyApplication.mLogInDataBean.getUsername());
        mMap.put("strength", strength + "");
        setBaseOkgo("user/set-strength");
    }

    public void requestChangeUserInfo(Map<String, String> map) {
        requestMode = 3;
        map.put("username", MyApplication.mLogInDataBean.getUsername());
        mMap = map;
        setBaseOkgo("user/update-user-info");
    }

    private void saveUserData(LogInDataBean data) {
        // 保存到内存当中
        MyApplication.mLogInDataBean = data;

        data.setUid(1);
        // 将数据存储到数据库中
        mRealm.beginTransaction();
        mRealm.copyToRealmOrUpdate(data);
        mRealm.commitTransaction();
        setUserInfoSucceed();
    }


    public void setUserInfoSucceed() {

        requestMode = 4;

        mMap.clear();
        mMap.put("plan_id", getPlanId() + "");
        mMap.put("start_day", TimeUtils.getFetureDate(0));
        mMap.put("end_day", TimeUtils.getFetureDate(28));
        mMap.put("username", MyApplication.mLogInDataBean.getUsername());
        setBaseOkgo("plan/add-user-plan");
    }

    // 获取计划ID
    private int getPlanId() {
        int planId = 0;
        int sex = Integer.parseInt(MyApplication.mLogInDataBean.getGender());

        if (sex == 0) {
            // 女
            planId = mBelongType * 2 - 1;
        } else if (sex == 1) {
            planId = mBelongType * 2;
        }

        return planId;
    }
}
